﻿namespace VividKit.ComponentModel
{
    using System;
    using System.Threading.Tasks;
    using Windows.ApplicationModel.Core;
    using Windows.UI.Core;

    internal class Dispatcher : IDispatcher
    {
        async Task IDispatcher.InvokeAsync(Action action)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher
                .RunAsync(CoreDispatcherPriority.Low, () => action());
        }

        async Task<TResult> IDispatcher.InvokeAsync<TResult>(Func<TResult> callback)
        {
            var result = default(TResult);

            await CoreApplication.MainView.CoreWindow.Dispatcher
                .RunAsync(CoreDispatcherPriority.Low, delegate
            {
                result = callback();
            });

            return result;
        }
    }
}